import { HardwareModel, ConnectStatus } from './hardware-model'

export enum BatteryLevel {
  unknown,        //未知状态
  low,            //低电量
  one,            //一格电
  two,            //两格电
  three,          //三格电
  four,           //四格电
  low_charge,     // low power, charging
  one_charge,
  two_charge,
  three_charge,
  four_charge,
}

export class BatteryService {
  static level(batteryValue: string, chargingValue: string): BatteryLevel {
    let isCharging = (chargingValue == "1" || chargingValue == "true")
    if (isCharging) {
      if (batteryValue == "0") {
        return BatteryLevel.low_charge
      } else if (batteryValue == "1") {
        return BatteryLevel.low_charge
      } else if (batteryValue == "2") {
        return BatteryLevel.one_charge
      } else if (batteryValue == "3") {
        return BatteryLevel.two_charge
      } else if (batteryValue == "4") {
        return BatteryLevel.three_charge
      } else if (batteryValue == "5") {
        return BatteryLevel.four_charge
      } else {
        return BatteryLevel.unknown
      }
    }

    if (batteryValue == "0") {
      return BatteryLevel.low
    } else if (batteryValue == "1") {
      return BatteryLevel.one
    } else if (batteryValue == "2") {
      return BatteryLevel.two
    } else if (batteryValue == "3") {
      return BatteryLevel.three
    } else if (batteryValue == "4") {
      return BatteryLevel.four
    } else if (batteryValue == "5") {
      return BatteryLevel.four
    } else {
      return BatteryLevel.unknown
    }
  }

  static image(level: BatteryLevel): string {
    switch (level) {
      case BatteryLevel.low: {
        return "icon_battery6.png"
      }
      case BatteryLevel.one: {
        return "icon_battery4.png"
      }
      case BatteryLevel.two: {
        return "icon_battery3.png"
      }
      case BatteryLevel.three: {
        return "icon_battery2.png"
      }
      case BatteryLevel.four: {
        return "icon_battery1.png"
      }
      case BatteryLevel.low_charge: {
        return "icon_battery01.gif"
      }
      case BatteryLevel.one_charge: {
        return "icon_battery12.gif"
      }
      case BatteryLevel.two_charge: {
        return "icon_battery23.gif"
      }
      case BatteryLevel.three_charge: {
        return "icon_battery34.gif"
      }
      case BatteryLevel.four_charge: {
        return "icon_battery11.png"
      }
      default: {
        return "icon_battery6.png"
      }
    }
  }
}

export enum PmLevel {
  unknown,
  one,             //0-50      优
  two,             //50-100    良
  three,           //100-150   轻度污染
  /*four,            //150-200   中度污染
  five,            //200-300   重度污染
  six              //300-500   严重污染*/
}
//pm: 35-75
export class PmService {
  static level(pmValue: string): PmLevel {
    let pmInt = parseInt(pmValue)
    if (isNaN(pmInt) || pmInt < 0) {
      return PmLevel.unknown
    }
    if (pmInt == 0xffff) {
      return PmLevel.one
    }
    if (pmInt < 35) {
      return PmLevel.one
    } else if (pmInt < 75) {
      return PmLevel.two
    } else {
      return PmLevel.three
    }
  }

  static offlineClass(level: PmLevel): string {
    switch (level) {
      case PmLevel.one: {
        return "cube-blue-offline"
      }
      case PmLevel.two: {
        return "cube-yellow-offline"
      }
      case PmLevel.three: {
        return "cube-red-offline"
      }
      /*case PmLevel.four: {
        return "cube-red-offline"
      }
      case PmLevel.five: {
        return "cube-red-offline"
      }
      case PmLevel.six: {
        return "cube-red-offline"
      }*/
      default: {
        return "cube-unknown-offline"
      }
    }
  }

  static bgColorClass(level: PmLevel): string {
    switch (level) {
      case PmLevel.one: {
        return "cube-blue"
      }
      case PmLevel.two: {
        return "cube-yellow"
      }
      case PmLevel.three: {
        return "cube-red"
      }
      /*case PmLevel.four: {
        return "cube-red"
      }
      case PmLevel.five: {
        return "cube-red"
      }
      case PmLevel.six: {
        return "cube-red"
      }*/
      default: {
        return "cube-unknown"
      }
    }
  }

  static title(level: PmLevel): string {
    switch (level) {
      case PmLevel.one: {
        return "REFERENCE.GOOD"
      }
      case PmLevel.two: {
        return "REFERENCE.MODERATE"
      }
      case PmLevel.three: {
        return "REFERENCE.UNHEALTHY"
      }
      default: {
        return "REFERENCE.UNABLE_TO_OBTAIN_DATA"
      }
    }

  }
}
export enum HchoLevel {
  unknown,
  normal,
  over,
  serious
}

export class HchoService {
  static level(hchoValue: string): HchoLevel {
    let hchoFloat = parseFloat((parseInt(hchoValue) / 1000).toFixed(2))
    if (isNaN(hchoFloat) || parseInt(hchoValue) < 0) {
      return HchoLevel.unknown
    }
    if (parseInt(hchoValue) == 0xffff) {
      return HchoLevel.normal
    }
    if (hchoFloat < 0.08) {
      return HchoLevel.normal
    } else if (hchoFloat < 0.10) {
      return HchoLevel.over
    } else {
      return HchoLevel.serious
    }
  }

  static offlineClass(level: HchoLevel): string {
    switch (level) {
      case HchoLevel.normal: {
        return "cube-blue-offline"
      }
      case HchoLevel.over: {
        return "cube-yellow-offline"
      }
      case HchoLevel.serious: {
        return "cube-red-offline"
      }
      default: {
        return "cube-unknown-offline"
      }
    }
  }
  static bgColorClass(level: HchoLevel): string {
    switch (level) {
      case HchoLevel.normal: {
        return "cube-blue"
      }
      case HchoLevel.over: {
        return "cube-yellow"
      }
      case HchoLevel.serious: {
        return "cube-red"
      }
      default: {
        return "cube-unknown"
      }
    }
  }

  static title(level: HchoLevel): string {
    switch (level) {
      case HchoLevel.normal: {
        return "REFERENCE.NORMAL"
      }
      case HchoLevel.over: {
        return "REFERENCE.EXCEEDED"
      }
      case HchoLevel.serious: {
        return "REFERENCE.SERIOUSLY_EXCEEDED"
      }
      default: {
        return "REFERENCE.UNABLE_TO_OBTAIN_DATA"
      }
    }
  }
}

export class CubeModel extends HardwareModel {
  //private cube variables
  private pm25 = ""
  private pm10 = ""
  private pm1_0 = ""
  private hcho = ""
  private battery = ""
  charging = ""
  samplePeriod = ""

  constructor(dict: object) {
    super(dict)
    this.pm25 = dict["pm25"] ? dict["pm25"] : ""
    this.hcho = dict["hcho"] ? dict["hcho"] : ""
    this.battery = dict["battery"] ? dict["battery"] : ""
    this.charging = dict["charging"] ? dict["charging"] : ""
    this.samplePeriod = dict["samplePeriod"] ? dict["samplePeriod"] : ""
  }

  //pm2.5 properties
  get pmLevel(): PmLevel {
    return PmService.level(this.pm25)
  }

  get pmBgClass(): string {
    if (this.connectStatus == ConnectStatus.disconnected) {
      return PmService.offlineClass(this.pmLevel)
    }
    return PmService.bgColorClass(this.pmLevel)
  }

  get pmTitle(): string {
    return PmService.title(this.pmLevel)
  }

  get pmFormated(): string {
    let pmInt = parseInt(this.pm25)
    if (isNaN(pmInt) || pmInt < 0) {
      return "--"
    }
    if (pmInt == 0xffff) {
      return "0"
    }
    return pmInt.toFixed()
  }
  //hcho properties
  get hchoLevel(): HchoLevel {
    return HchoService.level(this.hcho)
  }

  get hchoBgClass(): string {
    if (this.connectStatus == ConnectStatus.disconnected) {
      return HchoService.offlineClass(this.hchoLevel)
    }
    return HchoService.bgColorClass(this.hchoLevel)
  }

  get hchoTitle(): string {
    return HchoService.title(this.hchoLevel)
  }

  get hchoFormated(): string {
    let hchoInt = parseInt(this.hcho)
    if (isNaN(hchoInt) || hchoInt < 0) {
      return "--"
    }
    if (hchoInt == 0xffff) {
      return '0.00'
    }
    return (hchoInt / 1000).toFixed(2)
  }
  //battery properties
  get batteryLevel(): BatteryLevel {
    return BatteryService.level(this.battery, this.charging)
  }
  get batteryImage(): string {
    return BatteryService.image(this.batteryLevel)
  }

  setOffline() {
    this.status = ConnectStatus.disconnected.toString()
    this.charging = ""
  }
}
